package com.hhw.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hhw.entity.Product;
import com.hhw.util.DBUtil;


public class ProductDao {
		
	
	// 商品查询方法
		public List<Product> findAll() {

			try {

				List<Product> list = new ArrayList<Product>();
				Connection con=DBUtil.getConnection();
				PreparedStatement  ps = con.prepareStatement("select * from product");
			  ResultSet rs = ps.executeQuery();
				Product p = null;
				while (rs.next()) {
					p = new Product();
					 p.setId(rs.getInt(1));
					 p.setName(rs.getString(2));
					 p.setDescription(rs.getString(3));
					 p.setListimg(rs.getString(4));
					 p.setPrice(rs.getInt(5));
					 p.setDiscountprice(rs.getInt(6));
					list.add(p);
					System.out.println(p);
				}
				return list;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
	
		//
		public Product findProductById(int id) {
			// TODO Auto-generated method stub
			try {
				Connection conn = DBUtil.getConnection();
				PreparedStatement ps = conn.prepareStatement("select  * from product where id = ?");
				ps.setInt(1,id);
				ResultSet rs = ps.executeQuery();
				Product p = null;

				while (rs.next()) {
					p = new Product();
					 p.setId(rs.getInt(1));
					 p.setName(rs.getString(2));
					 p.setDescription(rs.getString(3));
					 p.setListimg(rs.getString(4));
					 p.setPrice(rs.getInt(5));
					 p.setDiscountprice(rs.getInt(6));
				}
				return p;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
	
		
		
		
		
		
	
	
	
	
}
